

use "Dataset_without_list.dta", clear

* remove 134 respondents that did not finishsca
* For robustness: check demographic differences, and check results with these respondents included
drop if Finished==0

* Mark missings on trait dummies
qui recode Q16_* (.=0)  if Q13_F!=.
qui recode Q23_* (.=0)  if Q20_F!=. 
qui recode Q30_* (.=0)  if Q27_F!=. 
qui recode Q37_* (.=0)  if Q34_F!=. 
qui recode Q44_* (.=0)  if Q41_F!=. 
qui recode Q51_* (.=0)  if Q48_F!=. 
qui recode Q58_* (.=0)  if Q55_F!=. 
qui recode Q65_* (.=0)  if Q62_F!=. 
qui recode Q72_* (.=0)  if Q69_F!=. 
qui recode Q79_* (.=0)  if Q76_F!=. 
qui recode Q86_* (.=0)  if Q83_F!=. 
qui recode Q93_* (.=0)  if Q90_F!=. 
qui recode Q100_* (.=0) if Q97_F!=. 
qui recode Q107_* (.=0) if Q104_F!=. 
qui recode Q114_* (.=0) if Q111_F!=. 
qui recode Q121_* (.=0) if Q118_F!=. 
qui recode Q128_* (.=0) if Q125_F!=. 
qui recode Q135_* (.=0) if Q132_F!=. 
qui recode Q142_* (.=0) if Q139_F!=. 
qui recode Q149_* (.=0) if Q146_F!=.

qui recode Q19_* (.=0)  if Q17_F!=.
qui recode Q26_* (.=0)  if Q24_F!=. 
qui recode Q33_* (.=0)  if Q31_F!=. 
qui recode Q40_* (.=0)  if Q38_F!=. 
qui recode Q47_* (.=0)  if Q45_F!=. 
qui recode Q54_* (.=0)  if Q52_F!=. 
qui recode Q61_* (.=0)  if Q59_F!=. 
qui recode Q68_* (.=0)  if Q66_F!=. 
qui recode Q75_* (.=0)  if Q73_F!=. 
qui recode Q82_* (.=0)  if Q80_F!=. 
qui recode Q89_* (.=0)  if Q87_F!=. 
qui recode Q96_* (.=0)  if Q94_F!=. 
qui recode Q103_* (.=0) if Q101_F!=. 
qui recode Q110_* (.=0) if Q108_F!=. 
qui recode Q117_* (.=0) if Q115_F!=. 
qui recode Q124_* (.=0) if Q122_F!=. 
qui recode Q131_* (.=0) if Q129_F!=. 
qui recode Q138_* (.=0) if Q136_F!=. 
qui recode Q145_* (.=0) if Q143_F!=. 
qui recode Q152_* (.=0) if Q150_F!=.

* Replace number of traits for trait word (up to 11 characters, no spaces)
foreach question of newlist Q16 Q23 Q30 Q37 Q44 Q51 Q58 Q65 Q72 Q79 Q86 Q93 Q100 Q107 Q114 Q121 Q128 Q135 Q142 Q149 {
	di "`question'"
	rename `question'_2     `question'_abenteuerlu
	rename `question'_3     `question'_herzlich
	rename `question'_5     `question'_ambitionier
	rename `question'_8     `question'_künstlerisc
	rename `question'_9     `question'_durchsetzun
	rename `question'_15    `question'_fürsorglich
	rename `question'_16    `question'_vorsichtig
	rename `question'_18    `question'_grob
	rename `question'_19    `question'_kühl
	rename `question'_20    `question'_respekteinf
	rename `question'_24    `question'_selbstsiche
	rename `question'_26    `question'_kreativ
	rename `question'_28    `question'_zynisch
	rename `question'_30    `question'_anständig
	rename `question'_32    `question'_unselbständ
	rename `question'_33    `question'_entschlosse
	rename `question'_39    `question'_gebildet
	rename `question'_40    `question'_egoistisch
	rename `question'_45    `question'_einfühlsam
	rename `question'_48    `question'_gutmitZahle
	rename `question'_52    `question'_abgebrüht
	rename `question'_57    `question'_bürgernah
	rename `question'_59    `question'_inspirieren
	rename `question'_60    `question'_intelligent
	rename `question'_62    `question'_sachkundig
	rename `question'_63    `question'_führungsper
	rename `question'_64    `question'_lügner
	rename `question'_70    `question'_mütterlich
	rename `question'_71    `question'_motiviert
	rename `question'_73    `question'_nörgelend
	rename `question'_74    `question'_objektiv
	rename `question'_75    `question'_organisiert
	rename `question'_77    `question'_machthungri
	rename `question'_80    `question'_mathematisc
	rename `question'_82    `question'_rational
	rename `question'_83    `question'_sorgensichu
	rename `question'_85    `question'_robust
	rename `question'_86    `question'_intrigant
	rename `question'_87    `question'_eigennützig
	rename `question'_89    `question'_sensibel
	rename `question'_90    `question'_unterwürfig
	rename `question'_92    `question'_klug
	rename `question'_93    `question'_hinterlisti
	rename `question'_95    `question'_streng
	rename `question'_97    `question'_willensstar
	rename `question'_98    `question'_aufopfernd
	rename `question'_101   `question'_taff
	rename `question'_106   `question'_warmherzig
	rename `question'_108   `question'_gutgebildet
	rename `question'_110   `question'_weinerlich
}	

foreach question of newlist Q19 Q26 Q33 Q40 Q47 Q54 Q61 Q68 Q75 Q82 Q89 Q96 Q103 Q110 Q117 Q124 Q131 Q138 Q145 Q152 {	
	rename `question'_1     `question'_aktiv
	rename `question'_4     `question'_aggressiv
	rename `question'_6     `question'_analytisch
	rename `question'_7     `question'_arrogant
	rename `question'_12     `question'_verbittert
	rename `question'_13     `question'_überheblich
	rename `question'_17     `question'_charismatis
	rename `question'_21     `question'_mitfühlend
	rename `question'_22     `question'_ehrgeizig
	rename `question'_23    `question'_jammernd
	rename `question'_25    `question'_korrupt
	rename `question'_29    `question'_wagemutig
	rename `question'_31    `question'_betrügerisc
	rename `question'_34    `question'_hinterhälti
	rename `question'_35    `question'_diktatorisc
	rename `question'_36    `question'_unehrlich
	rename `question'_37    `question'_dominant
	rename `question'_38    `question'_getrieben
	rename `question'_41    `question'_emotional
	rename `question'_42    `question'_ethisch
	rename `question'_44    `question'_kleinlich
	rename `question'_46    `question'_tatkräftig
	rename `question'_47    `question'_problemlösu
	rename `question'_50    `question'_gierig
	rename `question'_51    `question'_leichtgläub
	rename `question'_53    `question'_fleißig
	rename `question'_54    `question'_ehrlich
	rename `question'_55    `question'_feindselig
	rename `question'_56    `question'_phantasievo
	rename `question'_58    `question'_unabhängig
	rename `question'_61    `question'_intuitiv
	rename `question'_65    `question'_logisch
	rename `question'_66    `question'_liebevoll
	rename `question'_67    `question'_manipulativ
	rename `question'_69    `question'_integer
	rename `question'_78    `question'_mächtig
	rename `question'_81    `question'_streitsücht
	rename `question'_88    `question'_selbstsücht
	rename `question'_91    `question'_schmierig
	rename `question'_94    `question'_rückgratlos
	rename `question'_96    `question'_stark
	rename `question'_99    `question'_sympathisch
	rename `question'_100    `question'_gesprächig
	rename `question'_102    `question'_nichtinderl
	rename `question'_103    `question'_unemotional
	rename `question'_104    `question'_prinzipienl
	rename `question'_105    `question'_verklemmt
	rename `question'_107    `question'_schwach
	rename `question'_109    `question'_wortgewandt
}

* Replace question number for question category and group
foreach word of newlist _abenteuerlu _herzlich _ambitionier _künstlerisc _durchsetzun _fürsorglich _vorsichtig _grob _kühl _respekteinf _selbstsiche _kreativ _zynisch _anständig _unselbständ _entschlosse _gebildet _egoistisch _einfühlsam _gutmitZahle _abgebrüht _bürgernah _inspirieren _intelligent _sachkundig _führungsper _lügner _mütterlich _motiviert _nörgelend _objektiv _organisiert _machthungri _mathematisc _rational _sorgensichu _robust _intrigant _eigennützig _sensibel _unterwürfig _klug _hinterlisti _streng _willensstar _aufopfernd _taff _warmherzig _gutgebildet _weinerlich {
	rename Q16`word' 	k_fempol`word'
	rename Q23`word' 	k_malpol`word'
	rename Q30`word' 	k_allpol`word'
	rename Q37`word' 	k_women`word'
	rename Q44`word' 	k_men`word'
	rename Q51`word' 	e_fempol`word'
	rename Q58`word' 	e_malpol`word'
	rename Q65`word' 	e_allpol`word'
	rename Q72`word' 	e_women`word'
	rename Q79`word' 	e_men`word'
	
	qui replace k_fempol`word'   = Q86`word'  if k_fempol`word'==. 	
	qui replace k_malpol`word'   = Q93`word'  if k_malpol`word'==. 
	qui replace k_allpol`word'   = Q100`word' if k_allpol`word'==. 
	qui replace k_women`word'    = Q107`word' if k_women`word' ==. 
	qui replace k_men`word'      = Q114`word' if k_men`word'   ==. 
	qui replace e_fempol`word'   = Q121`word' if e_fempol`word'==. 
	qui replace e_malpol`word'   = Q128`word' if e_malpol`word'==. 
	qui replace e_allpol`word'   = Q135`word' if e_allpol`word'==. 
	qui replace e_women`word'    = Q142`word' if e_women`word' ==. 
	qui replace e_men`word'	     = Q149`word' if e_men`word'	==. 
}   

foreach word of newlist _aktiv _aggressiv _analytisch _arrogant _verbittert _überheblich _charismatis _mitfühlend _ehrgeizig _jammernd _korrupt _wagemutig _betrügerisc _hinterhälti _diktatorisc _unehrlich _dominant _getrieben _emotional _ethisch _kleinlich _tatkräftig _problemlösu _gierig _leichtgläub _fleißig _ehrlich _feindselig _phantasievo _unabhängig _intuitiv _logisch _liebevoll _manipulativ _integer _mächtig _streitsücht _selbstsücht _schmierig _rückgratlos _stark _sympathisch _gesprächig _nichtinderl _unemotional _prinzipienl _verklemmt _schwach _wortgewandt {
	rename Q19`word' 	k_fempol`word'
	rename Q26`word' 	k_malpol`word'
	rename Q33`word' 	k_allpol`word'
	rename Q40`word' 	k_women`word'
	rename Q47`word' 	k_men`word'
	rename Q54`word' 	e_fempol`word'
	rename Q61`word' 	e_malpol`word'
	rename Q68`word' 	e_allpol`word'
	rename Q75`word' 	e_women`word'
	rename Q82`word' 	e_men`word'
	
	qui replace k_fempol`word'   = Q89`word'  if k_fempol`word'==. 	
	qui replace k_malpol`word'   = Q96`word'  if k_malpol`word'==. 
	qui replace k_allpol`word'   = Q103`word' if k_allpol`word'==. 
	qui replace k_women`word'    = Q110`word' if k_women`word' ==. 
	qui replace k_men`word'      = Q117`word' if k_men`word'   ==. 
	qui replace e_fempol`word'   = Q124`word' if e_fempol`word'==. 
	qui replace e_malpol`word'   = Q131`word' if e_malpol`word'==. 
	qui replace e_allpol`word'   = Q138`word' if e_allpol`word'==. 
	qui replace e_women`word'    = Q145`word' if e_women`word' ==. 
	qui replace e_men`word'	     = Q152`word' if e_men`word'	==. 
}

* Simplify the dataset a little, all groups in one variable rather than one per group 
gen questiontype = 0
replace questiontype = 1 if k_fempol_aktiv!=.
replace questiontype = 2 if k_malpol_aktiv!=.
replace questiontype = 3 if k_allpol_aktiv!=.
replace questiontype = 4 if k_women_aktiv!=.
replace questiontype = 5 if k_men_aktiv!=.
replace questiontype = 6 if e_fempol_aktiv!=.
replace questiontype = 7 if e_malpol_aktiv!=.
replace questiontype = 8 if e_allpol_aktiv!=.
replace questiontype = 9 if e_women_aktiv!=.
replace questiontype = 10 if e_men_aktiv!=.
label define questiontypelbl 1 "k_fempol_" 2 "k_malpol_" 3 "k_allpol_" 4 "k_women_" 5 "k_men_" 6 "e_fempol_" 7 "e_malpol_" 8 "e_allpol_" 9 "e_women_" 10 "e_men_"
label val questiontype questiontypelbl

foreach word of newlist _aktiv _aggressiv _analytisch _arrogant _verbittert _überheblich _charismatis _mitfühlend _ehrgeizig _jammernd _korrupt _wagemutig _betrügerisc _hinterhälti _diktatorisc _unehrlich _dominant _getrieben _emotional _ethisch _kleinlich _tatkräftig _problemlösu _gierig _leichtgläub _fleißig _ehrlich _feindselig _phantasievo _unabhängig _intuitiv _logisch _liebevoll _manipulativ _integer _mächtig _streitsücht _selbstsücht _schmierig _rückgratlos _stark _sympathisch _gesprächig _nichtinderl _unemotional _prinzipienl _verklemmt _schwach _wortgewandt _abenteuerlu _herzlich _ambitionier _künstlerisc _durchsetzun _fürsorglich _vorsichtig _grob _kühl _respekteinf _selbstsiche _kreativ _zynisch _anständig _unselbständ _entschlosse _gebildet _egoistisch _einfühlsam _gutmitZahle _abgebrüht _bürgernah _inspirieren _intelligent _sachkundig _führungsper _lügner _mütterlich _motiviert _nörgelend _objektiv _organisiert _machthungri _mathematisc _rational _sorgensichu _robust _intrigant _eigennützig _sensibel _unterwürfig _klug _hinterlisti _streng _willensstar _aufopfernd _taff _warmherzig _gutgebildet _weinerlich {
	egen tot`word' = rowtotal(k_*`word' e_*`word')
}
keep questiontype tot_* 

* Define scales
do "Globals scales.do"


* Kinder leadership dimensions scales
capture drop dk_*
foreach i of newlist _com _lea _emp _int {
	gen dk`i' = 0
	local count = 0
	foreach trait of global kinder`i' {
		replace dk`i' = dk`i' + tot_`trait'
		local count = `count' + 1
	}
	replace dk`i' = dk`i' / `count'
}

* Psychology trait scales
capture drop d_*
foreach i of newlist _agentic _communal _eaglyagen _eaglycomp {
	gen d`i' = 0
	local count = 0
	foreach trait of global d`i' {
		replace d`i' = d`i' + tot_`trait'
		local count = `count' + 1
	}
	replace d`i' = d`i' / `count'
}


* copy vars so as to easily create SE in collapse command
foreach var of varlist tot_* {
	local tempname = regexr("`var'","tot","se") 
	gen `tempname' = `var'
}
foreach var of varlist dk_* d_* {
	gen se_`var' = `var'
	gen n_`var' = `var'
	rename `var' tot_`var'
}

* Summarize to means and standard errors
collapse (mean) tot_* (semean) se_* (count) n_*, by(questiontype)

* Reshape, along the way store mean trait score and sd for standardization later
reshape long tot_ se_ n_, j(trait) i(questiontype) string
decode questiontype, gen(questionstring)
drop questiontype
qui sum tot_ if !regex(trait,"d_") & !regex(trait,"dk_") & regex(questionstring,"k_")
global meantrait_k = r(mean)
global sdtrait_k   = r(sd)
qui sum tot_ if !regex(trait,"d_") & !regex(trait,"dk_") & regex(questionstring,"e_")
global meantrait_e = r(mean)
global sdtrait_e   = r(sd)
reshape wide tot_ se_ n_, i(trait) j(questionstring) string

* format numvers
format tot_* se_* n_* %3.2f

* Rename variables
foreach var of varlist tot_* {
	local tempname = regexr("`var'","tot_","") 
	rename `var' `tempname'us
}
foreach var of varlist se_* n_* {
	rename `var' `var'us
}

* Store data, trait percentages
preserve
	drop if regex(trait,"dk_") | regex(trait,"d_")
	save "knowledgepercentages_US.dta", replace
restore

* Create lower and upper bound scale variables
foreach group of newlist fempol malpol allpol men women {
	gen klb_`group'_us = k_`group'_us + se_k_`group'_us * invt(n_k_`group'_us,0.025)
	gen kub_`group'_us = k_`group'_us - se_k_`group'_us * invt(n_k_`group'_us,0.025)
	gen elb_`group'_us = e_`group'_us + se_e_`group'_us * invt(n_e_`group'_us,0.025)
	gen eub_`group'_us = e_`group'_us - se_e_`group'_us * invt(n_e_`group'_us,0.025)
}

keep if regex(trait,"dk_") | regex(trait,"d_")
//drop n_* se_* 

rename (se_e_allpol_us se_e_fempol_us se_e_malpol_us se_e_men_us se_e_women_us se_k_allpol_us se_k_fempol_us se_k_malpol_us se_k_men_us se_k_women_us) (e_se_allpol_us e_se_fempol_us e_se_malpol_us e_se_men_us e_se_women_us k_se_allpol_us k_se_fempol_us k_se_malpol_us k_se_men_us k_se_women_us)
rename (n_e_allpol_us n_e_fempol_us n_e_malpol_us n_e_men_us n_e_women_us n_k_allpol_us n_k_fempol_us n_k_malpol_us n_k_men_us n_k_women_us) (e_n_allpol_us e_n_fempol_us e_n_malpol_us e_n_men_us e_n_women_us k_n_allpol_us k_n_fempol_us k_n_malpol_us k_n_men_us k_n_women_us)


* standardize variables (relative to mean trait percentage over all groups)
foreach var of varlist k_allpol* k_fempol* k_malpol* k_men* k_women* k*b_allpol* k*b_fempol* k*b_malpol* k*b_men* k*b_women* {
	gen `var'_std = (`var' - $meantrait_k) / $sdtrait_k
}
foreach var of varlist k_se_* {
	gen `var'_std = `var' / $sdtrait_k
}
foreach var of varlist e_allpol* e_fempol* e_malpol* e_men* e_women* e*b_allpol* e*b_fempol* e*b_malpol* e*b_men* e*b_women* {
	gen `var'_std = (`var' - $meantrait_e) / $sdtrait_e
}
foreach var of varlist e_se_* {
	gen `var'_std = `var' / $sdtrait_e
}

* format numvers
format e* k* %3.2f
 
* Store data leadership and gendered prescription scales
save "knowledgescalescores_US.dta", replace







